package com.pwc.testing;

import java.util.List;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

//import com.sun.jna.Structure.FFIType.size_t;

public class SeleniumAssert extends SeleniumBaseAction implements ISeleniumAssert {

	public SeleniumAssert(WebDriver driver, LogUtil log) {
		super(driver, log);

	}

	private String findElementText(String findType, String element, String attribute, String attributeValue) {
		List<WebElement> list = findElements(findType, element);
		if (list.size() >= 1) {
			WebElement ele = list.get(0);

			if (Attribute.TEXT.equalsIgnoreCase(attribute)) {
				return ele.getText();
			} else if (Attribute.CSS.equalsIgnoreCase(attribute)) {
				return ele.getCssValue(attributeValue);
			} else if (Attribute.CSS.equalsIgnoreCase(attribute)) {
				return ele.getAttribute(attributeValue);
			} else {
				return null;
			}
		} else {
			return null;
		}

	}

	public void assertEquals(String[] c) {

		String expected = c[c.length - 1];

		String actual = "";

		if (c.length == 5) {
			actual = findElementText(c[1], c[2], c[3], null);
		} else if (c.length == 6) {
			actual = findElementText(c[1], c[2], c[3], c[4]);
		} else {
			log.addLog("error in equals execute.");
		}

		if (expected.equals(actual)) {
			log.addLog("success, " + c[2] + "," + c[1] + " property :" + c[3] + "equals " + expected);
		} else {
			log.addLog("error, expected value is " + expected + " but, actual is " + actual);
			log.addLog("__________________________________________________________");
		}

	}

	public void assertContains(String[] c) {

		String expected = c[c.length - 1];

		String actual = "";

		if (c.length == 5) {
			actual = findElementText(c[1], c[2], c[3], null);
		} else if (c.length == 6) {
			actual = findElementText(c[1], c[2], c[3], c[4]);
			
		} else {
			log.addLog("error, in equals execute.");
			log.addLog("__________________________________________________________");
		}
		

		if (actual.contains(expected)) {
			log.addLog("success, " + c[2] + "," + c[1] + " property :" + c[3] + " contains " + expected);
		} else {
			log.addLog("error, expected value is " + expected + " but, should contain " + actual);
			log.addLog("__________________________________________________________");
		}

	}

	public void assertGreater(String[] c) {
		String expected = c[c.length - 1];

		String actual = "";

		if (c.length == 5) {
			actual = findElementText(c[1], c[2], c[3], null);
		} else if (c.length == 6) {
			actual = findElementText(c[1], c[2], c[3], c[4]);
		} else {
			log.addLog("error, in equals execute.");
			log.addLog("__________________________________________________________");
		}
		
		actual =removeStr(actual);
				
		try {
			if (Double.parseDouble(actual)>Double.parseDouble(expected)) {
				log.addLog("success, " + c[2] + "," + c[1] + " property :" + c[3] + " is greater than " + expected);
			} else {
				log.addLog("error,"+ actual + " is not greater than "+ expected);
				log.addLog("__________________________________________________________");
			}
		} catch (NumberFormatException e) {
			log.addLog("error,"+ actual+ " cannot be parsed to long");
			log.addLog("__________________________________________________________");
		}
		
	}

	public void assertLess(String[] c) {
		String expected = c[c.length - 1];

		String actual = "";

		if (c.length == 5) {
			actual = findElementText(c[1], c[2], c[3], null);
		} else if (c.length == 6) {
			actual = findElementText(c[1], c[2], c[3], c[4]);
		} else {
			log.addLog("error, in equals execute.");
			log.addLog("__________________________________________________________");
		}

		actual =removeStr(actual);
		
		
		
		try {
			if (Double.parseDouble(actual)<Double.parseDouble(expected)) {
				log.addLog("success, " + c[2] + "," + c[1] + " property :" + c[3] + " is less than " + expected);
			} else {
				log.addLog("error,"+ actual + " is not less than "+ expected);
				log.addLog("__________________________________________________________");
			}
		} catch (NumberFormatException e) {
			log.addLog("error,"+ actual+ " cannot be parsed to long");
		}
	}
	
	
	private String removeStr(String str)
	{
		return str.replace("px", "");
	}

}
